﻿/*
 * 创建人：盛金龙
 * 时间：2011/12/27 周二
 * 用途：用户管理
 */
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;
using EnvironmentManagerSystem.MODE;
using EnvironmentManagerSystem.Utility;

namespace EnvironmentManagerSystem.UserManager
{
    public partial class UserManager : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (StfInfo.ReSession()) return;if (!IsPostBack)
            {
                //是否是管理员并操作isAdmin
                if (!(Boolean)Session["Admin"])
                {
                    gdvUser.Columns[gdvUser.Columns.Count - 1].Visible = false;
                    divInsert.Visible = false;
                }
                BingGdvUser();
                BingList();
            }
        }

        /// <summary>
        /// 绑定用户列表
        /// </summary>
        public void BingGdvUser()
        {
            gdvUser.DataSource = EnvironmentManagerSystem.BLL.User.GetUser((Boolean)Session["Admin"], hidUserName.Value, hidUserType.Value, hidRoleType.Value);
            gdvUser.DataBind();
        }

        //分页事件
        protected void gdvUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gdvUser.PageIndex = e.NewPageIndex;
        }

        //分页后事件
        protected void gdvUser_PageIndexChanged(object sender, EventArgs e)
        {
            BingGdvUser();
        }

        //编辑事件
        protected void gdvUser_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gdvUser.EditIndex = e.NewEditIndex;
            BingGdvUser();
        }

        //取消事件
        protected void gdvUser_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gdvUser.EditIndex = -1;
            BingGdvUser();
        }

        //更新事件
        protected void gdvUser_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (Check(e.RowIndex))
            {
                UserData use = new UserData();
                use.UserName = ((TextBox)gdvUser.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim();
                use.LoginName = ((TextBox)gdvUser.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();
                use.Password = ((TextBox)gdvUser.Rows[e.RowIndex].Cells[2].FindControl("TextBox1")).Text.Trim();
                use.Phone = ((TextBox)gdvUser.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();
                use.RoleId = gdvUser.Rows[e.RowIndex].Cells[4].Text;
                use.UserKind = ((TextBox)gdvUser.Rows[e.RowIndex].Cells[5].Controls[0]).Text.Trim();
                use.InUse = ((CheckBox)gdvUser.Rows[e.RowIndex].Cells[6].FindControl("CheckBox1")).Checked ? 1 : 0;
                use.ID = int.Parse(gdvUser.DataKeys[e.RowIndex].Value.ToString());
                if (EnvironmentManagerSystem.BLL.User.UpdateUser(use))
                {
                    gdvUser.EditIndex = -1;
                    BingGdvUser();
                    Jscript.Alert("修改成功", this);
                }
                else
                {
                    Jscript.Alert("用户名或登录名已存在", this);
                }
            }
        }

        //删除事件
        protected void gdvUser_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            EnvironmentManagerSystem.BLL.User.DeleteUser(gdvUser.DataKeys[e.RowIndex].Value.ToString());
            BingGdvUser();
        }

        /// <summary>
        /// 绑定用户角色
        /// </summary>
        public void BingList()
        {
            ddlRoleType.DataSource = EnvironmentManagerSystem.BLL.User.GetRole();
            ddlRoleType.DataBind();
            ddlRoleType.Items.Insert(0, new ListItem("请选择", ""));
            ddlUserType.DataSource = EnvironmentManagerSystem.BLL.User.GetUserType();
            ddlUserType.DataBind();
            ddlUserType.Items.Insert(0, new ListItem("请选择", ""));
        }

        /// <summary>
        /// 验证文本框
        /// </summary>
        public Boolean Check(int i)
        {
            if (!((TextBox)gdvUser.Rows[i].Cells[0].Controls[0]).Text.Trim().Equals("")
                && !((TextBox)gdvUser.Rows[i].Cells[1].Controls[0]).Text.Trim().Equals("")
                && !((TextBox)gdvUser.Rows[i].Cells[2].FindControl("TextBox1")).Text.Trim().Equals("")
                //&& !((TextBox)gdvUser.Rows[i].Cells[5].Controls[0]).Text.Trim().Equals("")
                )
            {
                if (Regex.IsMatch(((TextBox)gdvUser.Rows[i].Cells[3].Controls[0]).Text.Trim(), @"^[0-9]*$"))
                {
                    return true;
                }
                else
                {
                    Jscript.Alert("电话号码输入错误", this);
                    return false;
                }
            }
            else
            {
                //Jscript.Alert("用户名,密码,登陆名,类型都不能为空", this);
                Jscript.Alert("用户名,密码,登陆名", this);
                return false;
            }
        }

        //刷新数据
        protected void Button3_Click(object sender, EventArgs e)
        {
            BingGdvUser();
            BingList();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            hidUserName.Value = txtUserName.Text.Trim();
            if (ddlRoleType.SelectedIndex != 0)
                hidRoleType.Value = ddlRoleType.SelectedItem.Text;
            else
                hidRoleType.Value = string.Empty;
            if (ddlUserType.SelectedIndex != 0)
                hidUserType.Value = ddlUserType.SelectedItem.Text;
            else
                hidUserType.Value = string.Empty;
            BingGdvUser();
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtUserName.Text = string.Empty;
            ddlUserType.SelectedIndex = 0;
            ddlRoleType.SelectedIndex = 0;
        }

        protected void gdvUser_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            OfficeHelper.HelpDv(e.Row);
        }
        //鼠标滑动
        protected void gdvUser_DataBound(object sender, EventArgs e)
        {
            OfficeHelper.HelpDv(gdvUser);
        }
    }
}
